#include "player_vs_player.hpp"

namespace gomoku{

    player_vs_player::player_vs_player(int height, int width):game{height,width}{

    }

    player_vs_player::player_vs_player(int height, int width,int init_status, point_status *init_layout):game{height,width,init_status,init_layout}{

    }

    // 如果棋盘上出现一下特征，则黑方获胜
    std::vector<pattern> player_vs_player::winning_patterns_black{
                { // 连成对角线
                    {false,{-1,-1},point_status::black},
                    {true,{0,0},point_status::black},
                    {true,{1,1},point_status::black},
                    {true,{2,2},point_status::black},
                    {true,{3,3},point_status::black},
                    {true,{4,4},point_status::black},
                    {false,{5,5},point_status::black}
                },
                { // 连成对角线
                    {false,{-1,6},point_status::black},
                    {true,{0,5},point_status::black},
                    {true,{1,4},point_status::black},
                    {true,{2,3},point_status::black},
                    {true,{3,2},point_status::black},
                    {true,{4,1},point_status::black},
                    {false,{5,0},point_status::black}
                },
                { // 连成竖线
                    {false,{-1,0},point_status::black},
                    {true,{0,0},point_status::black},
                    {true,{1,0},point_status::black},
                    {true,{2,0},point_status::black},
                    {true,{3,0},point_status::black},
                    {true,{4,0},point_status::black},
                    {false,{5,0},point_status::black}
                },
                { // 连成横线
                    {false,{0,-1},point_status::black},
                    {true,{0,0},point_status::black},
                    {true,{0,1},point_status::black},
                    {true,{0,2},point_status::black},
                    {true,{0,3},point_status::black},
                    {true,{0,4},point_status::black},
                    {false,{0,5},point_status::black}
                }
    };

    // 道理同上
    std::vector<pattern> player_vs_player::winning_patterns_white{
                {
                    {false,{-1,-1},point_status::white},
                    {true,{0,0},point_status::white},
                    {true,{1,1},point_status::white},
                    {true,{2,2},point_status::white},
                    {true,{3,3},point_status::white},
                    {true,{4,4},point_status::white},
                    {false,{5,5},point_status::white}
                },
                {
                    {false,{-1,6},point_status::white},
                    {true,{0,5},point_status::white},
                    {true,{1,4},point_status::white},
                    {true,{2,3},point_status::white},
                    {true,{3,2},point_status::white},
                    {true,{4,1},point_status::white},
                    {false,{5,0},point_status::white}
                },
                {
                    {false,{-1,0},point_status::white},
                    {true,{0,0},point_status::white},
                    {true,{1,0},point_status::white},
                    {true,{2,0},point_status::white},
                    {true,{3,0},point_status::white},
                    {true,{4,0},point_status::white},
                    {false,{5,0},point_status::white}
                },
                {
                    {false,{0,-1},point_status::white},
                    {true,{0,0},point_status::white},
                    {true,{0,1},point_status::white},
                    {true,{0,2},point_status::white},
                    {true,{0,3},point_status::white},
                    {true,{0,4},point_status::white},
                    {false,{0,5},point_status::white}
                }
    };

    bool player_vs_player::do_operation(point position){
        if (board[position]!=point_status::vacant){ // 如果格子非空则操作失败，无法走棋
            return false;
        }
        _undone = false;
        switch (_status){
            case 0: // 黑方走棋
                board.place_black(position);
                break;
            case 1: // 白方走棋
                board.place_white(position);
                break;
        }
        // 下面轮到另一名玩家走棋了
        ++_status;
        _status %= 2;
        return true;
    }
    
    player player_vs_player::winner() const{
    for (auto i:winning_patterns_black){
        if (i.find(this->board)!=point{-1,-1}){
            return player::black;
        }
    }
    for (auto i:winning_patterns_white){
        if (i.find(this->board)!=point{-1,-1}){
            return player::white;
        }
    }
    return player::nobody;
    }

    bool player_vs_player::undo_operation(){
        if (_undone || this->board.change_log.empty()){
            return false;
        }else{
            this->board.clear_point(this->board.change_log.back().pos);
            _undone = true;
            ++_status;
            _status %= 2;
            return true;
        }
    }

}
